package com.diamond.webservice.service.impl;

import cn.hutool.core.date.DateUtil;
import com.diamond.webservice.util.MyDateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.diamond.webservice.entity.*;
import com.diamond.webservice.service.RequestMessageService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RequestMessageServiceImpl implements RequestMessageService {
    /**
     * 构造传输接口的请求报文头
     * @return
     */
    private header initTransmissionHeader(){
        header header = new header();
        header.setServiceCode("32000061191");
        header.setVersion("1.0");
        header.setSourceCode("3200011AA001");   // 调用方系统编码
        header.setApiCode("392-0001");
        header.setTimestamp(DateUtil.format(new Date(), "yyyyMMddHHmmss"));    //
        header.setSignature("");    // 数字签名
        return header;
    }

    /**
     * 构造传输接口的请求报文体
     * @return
     */
    private body initTransmissionBody(List<JsEcInfo> jsEcInfoList){
        body body = new body();
        body.setLicence(initTransmissionLicence(jsEcInfoList));
        body.setAppKey("f4631f1186664628a62b4ca5478d0717"); // 由电子证照系统分配
        body.setName("测试姓名");
        body.setUserName("测试用户名");
        body.setIp("127.0.0.1");
        return body;
    }

    /**
     * 构造传输接口的证照信息
     * @param jsEcInfoList
     * @return
     */
    private List<Licence<ZJCCertificate,ZJCExtend>> initTransmissionLicence(List<JsEcInfo> jsEcInfoList){
        List<Licence<ZJCCertificate,ZJCExtend>> licenceList = new ArrayList();
        for (JsEcInfo jsEcInfo : jsEcInfoList) {
            // 目录信息
            Catalog catalog = new Catalog();
            catalog.setHyCode("RS");    // 行业名称：固定值“RS”
            catalog.setZzName("RS08");  // 证照名称：固定值“RS08” 专业技术资格证书
            catalog.setYwName("专业技术资格证书");    // 业务名称：固定值，”向获得高级职称的专业技术人员发放证书"
            catalog.setIsProvince("1"); // 行使层级（省级）
            catalog.setIsCity("1"); // 行使层级（市级）
            catalog.setIsCounty("0"); // 行使层级（级）
            catalog.setZzbh(jsEcInfo.getCertificateNo());   // 证照编号
            String BzDate = "";
            try {
                BzDate = MyDateUtil.formatDate(jsEcInfo.getGetDateTime(),new SimpleDateFormat("yyyyMMdd"));
            }catch (Exception e){
                e.printStackTrace();
            }
            catalog.setBzDate(BzDate);
            catalog.setDeptName(StringUtils.isEmpty(jsEcInfo.getCertificateUnit())?"江苏省人力资源和社会保障厅":jsEcInfo.getCertificateUnit()); // 颁证单位
            catalog.setUserName(jsEcInfo.getName());    // 持证者
            catalog.setUserType("01");  // 持证者类型：固定值“01”，自然人
            catalog.setLicenceType("05");   // 持证者证件类型：固定值“05” 身份证
            catalog.setUserNo(jsEcInfo.getPaperNum());  //持证者证件号码
            catalog.setZzType("A"); // 固定值“A”
            catalog.setZzmbh("RS08ZJC003"); // 固定值 专业技术资格证书（省高级新)
            catalog.setXzCode("000"); // 行政区划代码
            catalog.setOrgName("jszjc001"); // 组织机构代码
            // 照面信息（专业技术资格证书）
            ZJCCertificate zjcCertificate = new ZJCCertificate();
//            FilePhoto filePhoto = new FilePhoto();
//            filePhoto.setPhoto(StringUtils.isEmpty(EncryptionUtil.getBase64Str(jsEcInfo.getUrlPath()))?"":EncryptionUtil.getBase64Str(jsEcInfo.getUrlPath()));
//            filePhoto.setPhotoName(UUID.randomUUID().toString()+".jpg");
//            zjcCertificate.setGRZP(filePhoto);  // 个人照片
            zjcCertificate.setXB(jsEcInfo.getSex()==0?"男":"女"); // 性别
            zjcCertificate.setGZDW(jsEcInfo.getDept()); //工作单位
            zjcCertificate.setSBPSZC(jsEcInfo.getNowQualification());    //审报评审职称(技术资格)
            zjcCertificate.setZSSYDW(jsEcInfo.getCertificateUser());    // 证书使用单位
            zjcCertificate.setGBWH(jsEcInfo.getAnnouncementNum());  //公布文号
            zjcCertificate.setSSXL(jsEcInfo.getSeries());   //送审系列
            zjcCertificate.setSSZY(jsEcInfo.getMajor());    //送审专业
            zjcCertificate.setPWHMC(jsEcInfo.getJuryName());    //评委会名称
            String birthday = "";
            String FZRQ = "";
            try {
                birthday = MyDateUtil.formatDate(jsEcInfo.getBirthday(),new SimpleDateFormat("yyyyMM"));
                FZRQ = MyDateUtil.formatDate(jsEcInfo.getGetDateTime(),new SimpleDateFormat("yyyyMMdd"));
            }catch (Exception e){
                e.printStackTrace();
            }
            zjcCertificate.setCSRQ(birthday);   // 出生日期
            zjcCertificate.setFZJG("江苏省人力资源和社会保障厅");
            zjcCertificate.setFZRQ(FZRQ);
            // 扩展信息
            ZJCExtend zjcExtend = new ZJCExtend();
            zjcExtend.setLXDH("");
            // 批文信息
//            ApprovalDoc approvalDoc = new ApprovalDoc();
            Licence<ZJCCertificate,ZJCExtend> licence = new Licence();
            licence.setCatalog(catalog);
            licence.setIndividuation(zjcCertificate);
            licence.setExtend(zjcExtend);
//            licence.setApprovalDoc(approvalDoc);
            licenceList.add(licence);
        }
        return licenceList;
    }

    /**
     * 构造传输接口的请求报文
     * @param jsEcInfoList
     * @return
     */
    public String initTransmissionParam(List<JsEcInfo> jsEcInfoList) {
        Request request = new Request();
        request.setHeader(initTransmissionHeader());
        body body = initTransmissionBody(jsEcInfoList);
        request.setBody(body);
//        String bodyJson = JSON.toJSONString(body);
//        // 对body的json字符串进行SM4加密
//        SymmetricCrypto sm4 = SmUtil.sm4();
//        String bodySalt = sm4.encryptHex(bodyJson);
//        request.setBody(bodyJson);
        String s = JSON.toJSONString(request, SerializerFeature.PrettyFormat);
        return s;
    }
}
